package com.wingsofts.gankclient

import android.app.Application
import android.content.Context
import com.alibaba.android.arouter.launcher.ARouter
import com.orhanobut.logger.LogAdapter
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import com.wingsofts.gankclient.di.component.ApiComponent
import com.wingsofts.gankclient.di.component.DaggerApiComponent
import com.wingsofts.gankclient.di.module.ApiModule
import com.wingsofts.gankclient.di.module.AppModule
import javax.inject.Inject

/**
 * Created by wing on 16-11-22.
 */
class App : Application() {

    init {
        instance = this
    }

    @Inject
    lateinit var apiComponent: ApiComponent

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
//        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()

        Logger.addLogAdapter(object : LogAdapter {
            private val formatStrategy = PrettyFormatStrategy
                    .newBuilder()
                    .tag("GankClient")
                    .build()

            override fun isLoggable(priority: Int, tag: String): Boolean {
                return BuildConfig.DEBUG
            }

            override fun log(priority: Int, tag: String, message: String) {
                formatStrategy.log(priority, tag, message)
            }
        })

        if (BuildConfig.DEBUG) {
            // 打印日志
            ARouter.openLog()
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug()
        }
        ARouter.init(this) // 尽可能早，推荐在Application中初始化

        DaggerApiComponent.builder()
                .apiModule(ApiModule())
                .appModule(AppModule(this))
                .build()
                .inject(this)
    }

    companion object {
        lateinit var instance: App
    }

}